/*
 * @Description  : document
 * @Autor        : 杜康
 * @Date         : 2021-08-14 08:12:22 +0800
 * @LastEditors  : 杜康
 * @LastEditTime : 2021-08-14 08:19:09 +0800
 */

/**
 * @description: 是否有class
 * @param {*} elem
 * @param {*} cls
 * @return {*}
 * @author: 杜康
 * @Date: 2021-08-14 08:16:51 +0800
 */
const hasClass = (elem, cls) => {
    cls = cls || "";
    if (cls.replace(/\s/g, "").length == 0) return false; //当cls没有参数时，返回false
    return new RegExp(" " + cls + " ").test(" " + elem.className + " ");
}

/**
 * @description: 添加class 依赖 hasClass
 * @param {*} elem
 * @param {*} cls
 * @return {*}
 * @author: 杜康
 * @Date: 2021-08-14 08:17:06 +0800
 */
const addClass = (elem, cls) => {
    if (!hasClass(elem, cls)) {
        elem.className =
            elem.className == "" ? cls : elem.className + " " + cls;
    }
}
/**
 * @description: 移出class 依赖 hasClass
 * @param {*} elem
 * @param {*} cls
 * @return {*}
 */
const removeClass = (elem, cls) => {
    if (hasClass(elem, cls)) {
        let newClass = " " + elem.className.replace(/[\t\r\n]/g, "") + " ";
        while (newClass.indexOf(" " + cls + " ") >= 0) {
            newClass = newClass.replace(" " + cls + " ", " ");
        }
        elem.className = newClass.replace(/^\s+|\s+$/g, "");
    }
}

/**
 * @description: 替换类名
 * @param {*} ele
 * @param {*} newName
 * @param {*} oldName
 * @return {*}
 * @author: 杜康
 * @Date: 2021-08-14 08:17:22 +0800
 */
const replaceClass = (ele, newName, oldName) => {
    this.removeClass(ele, oldName);
    this.addClass(ele, newName);
}

/**
 * @description: 获取兄弟节点
 * @param {*} ele
 * @return {*}
 * @author: 杜康
 * @Date: 2021-08-14 08:17:33 +0800
 */
const siblings = (ele) => {
    let chid = ele.parentNode.children,
        eleMatch = [];
    for (let i = 0, len = chid.length; i < len; i++) {
        if (chid[i] != ele) {
            eleMatch.push(chid[i]);
        }
    }
    return eleMatch;
}
// IE9+ window.getComputedStyle(element[,pseudo-element])

/**
 * @description: 获取元素最终样式
 * @param {*} el
 * @return {*}
 * @author: 杜康
 * @Date: 2021-08-14 08:17:43 +0800
 */
const getComputedStyles = (el) => {
    return el.ownerDocument.defaultView.getComputedStyle(el, null);
}

/**
 * @description: 键盘码
 * @author: 杜康
 * @Date: 2021-08-14 08:17:59 +0800
 */
let keyCodeMap = {
    8: "Backspace",
    9: "Tab",
    13: "Enter",
    16: "Shift",
    17: "Ctrl",
    18: "Alt",
    19: "Pause",
    20: "Caps Lock",
    27: "Escape",
    32: "Space",
    33: "Page Up",
    34: "Page Down",
    35: "End",
    36: "Home",
    37: "Left",
    38: "Up",
    39: "Right",
    40: "Down",
    42: "Print Screen",
    45: "Insert",
    46: "Delete",

    48: "0",
    49: "1",
    50: "2",
    51: "3",
    52: "4",
    53: "5",
    54: "6",
    55: "7",
    56: "8",
    57: "9",

    65: "A",
    66: "B",
    67: "C",
    68: "D",
    69: "E",
    70: "F",
    71: "G",
    72: "H",
    73: "I",
    74: "J",
    75: "K",
    76: "L",
    77: "M",
    78: "N",
    79: "O",
    80: "P",
    81: "Q",
    82: "R",
    83: "S",
    84: "T",
    85: "U",
    86: "V",
    87: "W",
    88: "X",
    89: "Y",
    90: "Z",

    91: "Windows",
    93: "Right Click",

    96: "Numpad 0",
    97: "Numpad 1",
    98: "Numpad 2",
    99: "Numpad 3",
    100: "Numpad 4",
    101: "Numpad 5",
    102: "Numpad 6",
    103: "Numpad 7",
    104: "Numpad 8",
    105: "Numpad 9",
    106: "Numpad *",
    107: "Numpad +",
    109: "Numpad -",
    110: "Numpad .",
    111: "Numpad /",

    112: "F1",
    113: "F2",
    114: "F3",
    115: "F4",
    116: "F5",
    117: "F6",
    118: "F7",
    119: "F8",
    120: "F9",
    121: "F10",
    122: "F11",
    123: "F12",

    144: "Num Lock",
    145: "Scroll Lock",
    182: "My Computer",
    183: "My Calculator",
    186: ";",
    187: "=",
    188: ",",
    189: "-",
    190: ".",
    191: "/",
    192: "`",
    219: "[",
    220: "\\",
    221: "]",
    222: "'",
};

/**
 * @description: 根据keycode获得键名
 * @param { Number } keycode
 * @return { String }
 * @author: 杜康
 * @Date: 2021-08-14 08:18:21 +0800
 */
const getKeycodeName = (keycode) => {
    if (keyCodeMap[keycode]) {
        return keyCodeMap[keycode];
    } else {
        console.log("Unknow Key(Key Code:" + keycode + ")");
        return "";
    }
}

/**
 * @description: 页面加载自执行函数
 * @param {*} func
 * @return {*}
 * @author: 杜康
 * @Date: 2021-08-14 08:18:53 +0800
 */
const addload = (func) => {
    let old = window.onload;
    if (typeof window.onload != "function") {
        window.onload = func;
    } else {
        window.onload = () => {
            old();
            func();
        };
    }
}

/**
 * @description: 获得文档的大小
 * @param {*}
 * @return {*}
 * @author: 杜康
 * @Date: 2021-08-14 08:19:07 +0800
 */
const getDocumentSize = () => {
    if (document.compatMode == "BackCompat") {
        return {
            width: document.body.scrollWidth,
            height: document.body.scrollHeight,
        };
    } else {
        return {
            width: Math.max(
                document.documentElement.scrollWidth,
                document.documentElement.clientWidth
            ),
            height: Math.max(
                document.documentElement.scrollHeight,
                document.documentElement.clientHeight
            ),
        };
    }
}

export default {
    hasClass,
    addClass,
    removeClass,
    replaceClass,
    getKeycodeName,
    siblings,
    addload,
    getComputedStyles,
    getDocumentSize,
};
